<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
file:///F:/PDC/ProjectEnv/workspace/Yule8xDocument/八星代理管理系统/toolkit/代码生成器.html
-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JFinal代码生成器</title>
<!-- Tell the browser to be responsive to screen width -->
<meta
	content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
	name="viewport" />
<style type="text/css">
body {
	font-size: 12px;
}
</style>
</head>
<body>
	<form id="form1" onsubmit="return false;">
		<div>
			SQL:<br />
			<textarea cols="120" rows="3">SELECT (col.name+','+typ.name) AS col1 FROM sys.columns col LEFT JOIN sys.types typ ON col.user_type_id=typ.user_type_id LEFT JOIN sys.tables tbl ON col.object_id=tbl.object_id WHERE tbl.name='t_user';</textarea>
		</div>
		<div>
			数据库字段：(field,type)<br />
			<textarea id="fields" cols="120" rows="15"></textarea>
			<br /> 代码类型： <input type="radio" name="type" value="model"
				checked="checked" />BaseDataModel <input type="radio" name="type" value="bean" />DataBean <input type="radio" name="type"
				value="form" />表单 <input type="radio" name="type" value="list" />列表
			<br />
			<button onclick="return generate()">立即生成</button>
		</div>
	</form>
	<textarea id="result" cols="120" rows="30"></textarea>
	<script type="text/javascript">
		// 2017-5-13
		function generate() {
			var fields = $id("fields").value;
			var type = $id("form1")["type"].value;
			if (!type || !fields) {
				alert('表单不完整!');
				return false;
			}
			var strout = "";
			switch (type) {
			case "model":
				strout = generate_model(fields);
				break;
			case "bean":
				strout = generate_bean(fields);
				break;
			case "form":
				strout = generate_form(fields);
				break;
			case "list":
				strout = generate_list(fields);
				break;
			}
			$id("result").value = strout;
			return false;
		}

		/**
		 * 生成模型
		 */
		function generate_model(fields) {
			var afields = fields.split("\n");
			var aitem;
			var strout = "", dtype, field, methodName, ln = "\r\n";
			for ( var i in afields) {
				aitem = afields[i].split(",");
				switch (aitem[1]) {
				case "int":
					dtype = "Integer";
					break;
				case "smallint":
					dtype = "Short";
					break;
				case "decimal":
					dtype = "java.math.BigDecimal";
					break;
				default:
					dtype = "String";
					break;
				}
				field = aitem[0];
				methodName = parseMethodName(field);
				strout += "public M set" + methodName + "(" + dtype + " value"
						+ ") {" + ln;
				strout += "\tset(\"" + field + "\", value);" + ln;
				strout += "\treturn (M)this;" + ln;
				strout += "}" + ln;
				strout += ln;
				strout += "public " + dtype + " get" + methodName + "() {" + ln;
				strout += "\treturn get(\"" + field + "\");" + ln;
				strout += "}" + ln;
			}
			return strout;
		}

		/**
		 * 生成Bean
		 */
		function generate_bean(fields) {
			var afields = fields.split("\n");
			var aitem;
			var stroutfields='';
			var strout = "", dtype, field, methodName, ln = "\r\n";
			for ( var i in afields) {
				aitem = afields[i].split(",");
				switch (aitem[1]) {
				case "int":
					dtype = "Integer";
					break;
				case "smallint":
					dtype = "Short";
					break;
				case "decimal":
					dtype = "java.math.BigDecimal";
					break;
				default:
					dtype = "String";
					break;
				}
				field = aitem[0];
				methodName = parseMethodName(field);
				strout += "public void set" + methodName + "(" + dtype + " value"
						+ ") {" + ln;
				strout += "\tthis." + field + "= value;" + ln;
				strout += "}" + ln;
				strout += ln;
				strout += "public " + dtype + " get" + methodName + "() {" + ln;
				strout += "\treturn this." + field + ";" + ln;
				strout += "}" + ln;
				stroutfields+="private "+dtype+" "+field+";"+ln;
			}
			return stroutfields+ln+strout;
		}
		/**
		 * 生成表单
		 */
		function generate_form(fields) {
			var strout = "";
			var afields = fields.split("\n");
			var aitem;
			var strout = "", field, ln = "\r\n";
			for ( var i in afields) {
				aitem = afields[i].split(",");
				field = aitem[0];
				strout += "<div class=\"form-group\">" + ln;
				strout += "\t<label class=\"col-sm-2 control-label\">名称</label>"
						+ ln;
				strout += "\t<div class=\"col-sm-10\">" + ln;
				strout += "\t<input type=\"text\" name=\""
						+ field
						+ "\" class=\"form-control input-sm\" placeholder=\"名称\" value=\"#(itemData."
						+ field + "??)\" />" + ln;
				strout += "\t</div>" + ln;
				strout += "</div>" + ln;
			}
			return strout;
		}
		/**
		 * 生成表格
		 */
		function generate_list(fields) {
			var strout = "";
			var afields = fields.split("\n");
			var aitem;
			var strth = "", strtd = "", dtype, field, methodName, ln = "\r\n";
			for ( var i in afields) {
				aitem = afields[i].split(",");
				field = aitem[0];
				strth += "<th title=\""+field+"\"></th>" + ln;
				strtd += "<td>#(x." + field + ")</td>" + ln;
			}
			return strth + ln + ln + strtd;
		}

		function parseMethodName(field) {
			var astrs = field.split('_');
			var strout = "";
			for ( var i in astrs) {
				strout += astrs[i][0].toUpperCase() + astrs[i].substring(1);
			}
			return strout;
		}
		function $id(id) {
			return document.getElementById(id);
		}
	</script>
</body>
</html>